package cn.jc.spider.neww.xiaohua;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import org.apache.torque.TorqueException;
import org.apache.torque.util.BasePeer;
import org.apache.torque.util.Criteria;

import com.workingdogs.village.DataSetException;
import com.workingdogs.village.Record;

import cn.jc.spider.po.DataXiaohua;
import cn.jc.spider.po.DataXiaohuaPeer;
import cn.jc.spider.util.Page;

public class WebService {

	public static List<DataXiaohua> queryXiaoHua(Page page) {
		Criteria c = new Criteria();
		c.add(DataXiaohuaPeer.ID, 1, Criteria.GREATER_THAN);//这里怎么测试table名？
		List<DataXiaohua> rs = null;
		try {
			page.setTotalcount(doselectcount(c));
			
			c.addDescendingOrderByColumn(DataXiaohuaPeer.URL);//这里的顺序要注意
			c.setLimit(page.getSize());
			c.setOffset(page.getStart());
			
			rs = DataXiaohuaPeer.doSelect(c);
		} catch (TorqueException e) {
			e.printStackTrace();
		} catch (DataSetException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return rs;
	}
	protected static int doselectcount(Criteria c) throws DataSetException, TorqueException, IOException, ClassNotFoundException {
		Criteria cc = (Criteria) copyObject(c);
		cc.addSelectColumn("count(*)");
		List baselist = BasePeer.doSelect(cc);
		Record sss = (Record) baselist.get(0);
		return sss.getValue(1).asInt();
	}
	public static Object copyObject(Object oldO) throws IOException,
	ClassNotFoundException {
		Object d = oldO;// 要写入的对象
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bo);
		oo.writeObject(d);
		bo.close();
		oo.close();
		
		ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
		ObjectInputStream oi = new ObjectInputStream(bi);
		Object o = oi.readObject();// 读出来的对象
		bi.close();
		oi.close();
		
// System.out.println(d==o);
// System.out.println(d.equals(o));
		Object newO = o;
		return newO;
	}
}
